function [ fitness_swarm ] = fitness_function( swarm , name_function )

[ no_particles, dim ] = size(swarm);

switch name_function
    
    case 'sphere'
        % ******** QUADRATIC: F(X) = x1^2 + x2^2 + . . .  + xn^2
        fitness_swarm = sum((swarm.^2)')';
        
        
    case 'rosenbrock'
        % ******** ROSENBROCK %
        % Equation -> sum ( 100 * (x(i+1) - x(i)^2)^2 + (1-x(i))^2 )
        %      xmin  = [1, 1, 1.....1]  (all ones)
        %      fxmin = 0                  (zero)        
        Swarm1 =  swarm(:, 1:(dim-1));
        Swarm2 =  swarm(:, 2:dim);
        if dim == 2
            fitness_swarm = 100 * (Swarm2 - Swarm1.^2).^2 + (1 - Swarm1).^2;
        else     
            fitness_swarm = sum((100 * (Swarm2 - Swarm1.^2).^2 + (1 - Swarm1).^2)')'; 
        end   
        
    case 'rastrigrin'
        % ******** RASTRIGIN: f(X) = [x1^2 - 10*cos(2*pi*x1) + 10] +  . . . + [xn^2 - 10*cos(2*pi*xn) + 10]
        % Equation ->  sum (x(i)^2 - 10 * cos(2 * pi * x(i)) + 10)
        %      xmin  = [0, 0, 0.....0]  (all zeoes)
        %      fxmin = 0                  (zero)        
        fitness_swarm = dim * 10 + sum(((swarm .^2) - 10 * cos(2 * pi * swarm))')';
        
    case 'griewank'        
        % ******** GRIEWANK
        fitness_swarm = ( sum(swarm'.^2)' ./ 4000 ) - prod( cos( swarm' ./ sqrt(repmat([1:dim],no_particles,1)') ) )' + 1 ;
        
        
    case 'schaffer_f6'        
        % ******* SCHAFFER f6 ********** 
        fitness_swarm = 0.5 + ((sin( sqrt(sum(swarm'.^2)') ) ).^2 - 0.5)./(1 + 0.001.*( sum(swarm'.^2)' )).^2;
        
        
    case 'shubert'        
        % ........ SHUBERT 18-global minima 2D ..........       
        fitness_swarm = ones(no_particles,1)*inf;
        s1 = 0; s2 = 0;
        for p=1:no_particles
            for i = 1:5;   
                s1 = s1+i*cos((i+1)*swarm(p,1)+i);
                s2 = s2+i*cos((i+1)*swarm(p,2)+i);
            end            
            fitness_swarm(p) = s1 * s2;            
            s1 = 0; s2 = 0;
        end
        
        
    case 'easom'        
        % EASOM global minima 2D X=(PI,PI) F=-1           
        fitness_swarm = ones(no_particles,1)*inf;        
        for p=1:no_particles         
            fitness_swarm(p) = -cos(swarm(p,1))*cos(swarm(p,2))*exp(-(swarm(p,1)-pi)^2-(swarm(p,2)-pi)^2);            
        end
        
        
    case 'branin'
        % BRANIN 3-global minima 2D X = (-? , 12.275), (? , 2.275), (9.42478, 2.475)
        % f(x*) = 0.397887
        fitness_swarm = ones(no_particles,1)*inf;
        for p=1:no_particles
            fitness_swarm(p) = ( swarm(p,2)-(5.1/(4*pi^2))*swarm(p,1)^2 + 5*swarm(p,1)/pi-6 )^2  +  10*(1-1/(8*pi)) * cos(swarm(p,1))  +  10;
        end


    case 'lattice01'
        fitness_swarm = evaluate(swarm);

    case 'volterra'
%         load dados_levitador.mat
        global ue ye
        N       = 20;
        ordem   = 2;
        H1  = zeros( 1, N+1 );
        H2  = zeros( 1, (N*(N-1)/2)+N );
        H   = [H1 H2];        
        [U linhas] = matriz_shift(ue,N);
        
        for p=1:no_particles
            Yaprox   = evaluate_volterra(swarm(p),U);            
            fitness_swarm(p) = sum(sum( ye(end-linhas+1:end)' - Yaprox ).^2) ;
        end
        


    otherwise
        disp('no function');
        
end